package com.lgx.right.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralJdbcDao;

import com.lgx.common.exception.DataException;
import com.lgx.common.tree.easyui.TreeModel;
import com.lgx.common.util.LayerOrderUtils;
import com.lgx.right.dao.IPermissionDao;
import com.lgx.right.entity.Permission;
import com.lgx.right.service.IPermissionService;

@Service("permissionService")
public class PermissionService implements IPermissionService {

	@Autowired
	private IPermissionDao permissionDao;
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	public IPermissionDao getPermissionDao() {
		return permissionDao;
	}

	public void setPermissionDao(IPermissionDao permissionDao) {
		this.permissionDao = permissionDao;
	}

	@Transactional
	public Permission createPermission(Permission permission)
			throws DataException {
		
		if(permission.getParent() != null)
		{
			String layerOrder = LayerOrderUtils.createLayerOrder("TS_PERMISSION", permission.getParent().getId()+"", this.baseDao);
			permission.setLayerOrder(layerOrder);
		}
		
		return permissionDao.createPermission(permission);
	}

	@Transactional
	public Permission updatePermission(Permission permission)
			throws DataException {
		return permissionDao.updatePermission(permission);
	}

	@Transactional
	public void deletePermission(Permission permission) throws DataException {
		permissionDao.deletePermission(permission);
	}

	@Transactional
	public void deletePermission(long id) throws DataException {
		permissionDao.deletePermission(id);
	}

	public Permission findPermissionById(long id) {
		return permissionDao.findPermissionById(id);
	}

	public List<Permission> findPermissionList(String sql) {
		return permissionDao.findPermissionList(sql);
	}

	public List<Permission> findPermissionList() {
		return this.permissionDao.findPermissionList();
	}

	public List<Permission> findPermissionList(long userId,String type) {
		return this.permissionDao.findPermissionList(userId, type);
	}

	public List<TreeModel> findPermissionTreeList() {
		return this.permissionDao.findPermissionTreeList();
	}

	public List<TreeModel> findPermissionRoleTreeList(long roleId) {
		return this.permissionDao.findPermissionRoleTreeList(roleId);
	}

	public List<Permission> findPermissionByIds(String ids) {
		return this.permissionDao.findPermissionByIds(ids);
	}
	
}
